#ifndef MODBUS_MASTER_H
#define MODBUS_MASTER_H

#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>
#include <QStringList>
#include <QThread>
#include "modbus.h"
#include <errno.h>

class Modbus_Master : public QObject
{
    Q_OBJECT
public:
    explicit Modbus_Master(int slave_id, QObject *parent = nullptr);
    ~Modbus_Master();

    void modbus_update_text();
    static void modbus_monitor_add_item( modbus_t * modbus,
                                         uint8_t is_out, uint8_t slave, uint8_t func, uint16_t addr,
                                         uint16_t nb, uint16_t expected_crc, uint16_t actual_crc );

    static void modbus_monitor_raw_data(modbus_t * modbus, uint8_t * data,
                                        uint8_t dataLen, uint8_t add_new_line,uint8_t direction);

signals:
    void signal_send_slave_info(QString str_display);

public slots:
    void open_poll_thread();
    void close_poll_thread();
    void modbus_master_poll_process();

    void connect_modbus_master(QString port_name, int baudrate);
    void disconnect_modbus_master();

private:
    QThread *m_master_thread;
    bool is_work;
    modbus_t *m_modbus = nullptr;
    modbus_mapping_t *mb_mapping = nullptr;
    QTimer *timer_one;
    int m_slave_id;
};

#endif // MODBUS_MASTER_H
